package com.example.friendfindertwo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import model.Event;

import com.example.friendfindertwo.application.StartApplication;
import com.example.friendfindertwo.data.helper.AbstractDAO;
import com.example.friendfindertwo.data.helper.CategoryDAO;
import com.friendfindertwo.control.EventListAdapter;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class EventListFragment extends ListFragment{

	private EventListAdapter adapter;
	private List<Event> mEventList;
	private ParseDatabase database;
	//private Dummies dummies;


	/**
	 * The adapter's current callback object, which is notified of edit button
	 * clicks.
	 */
	private Callbacks mCallbacks = sDummyCallbacks;

	/**
	 * A callback interface that all activities containing this fragment must
	 * implement. This mechanism allows activities to be notified of item
	 * selections.
	 */
	public interface Callbacks {
		/**
		 * Callback for when an item has been selected.
		 * @param id Id of the selected Element
		 */
		void onItemEditSelected(int id);
	}

	/**
	 * A dummy implementation of the {@link Callbacks} interface that does
	 * nothing. Used only when this fragment is not attached to an activity.
	 */
	private static Callbacks sDummyCallbacks = new Callbacks() {
		@Override
		public void onItemEditSelected(int id) {
		}
	};

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onStart()
	 */
	@Override
	public void onStart() {
		// TODO Auto-generated method stub

		super.onStart();

	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.ListFragment#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		if(adapter != null){
			adapter.setSelectedItem(position);
		}

		try{
			((IMenuListFragment)getActivity()).onMenuItemSelected(position);
		} catch (ClassCastException ce){
			Log.e("MenuListFragment", "Parent Activity does not implement required interfaces!");
		} catch (Exception e){
			Log.e("MenuListFragment", "Unhandled error!");
		}


	}

	public interface IMenuListFragment {
		public void onMenuItemSelected(int position);
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.ListFragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.i("onCreateView", "start function");

		super.onCreateView(inflater, container, savedInstanceState);

		database = new ParseDatabase();
		mEventList = new ArrayList<Event>();

		//			Friend friend = new Friend("fuend");
		//			AbstractDAO.open(getActivity().getApplicationContext());
		//
		//			FriendDAO friendDao = new FriendDAO();
		//			
		//			friendDao.createFriend(friend.getFriendName());
		//			
		//			CategoryDAO cat = new CategoryDAO(getActivity().getApplicationContext());
		//			cat.createCategoryDummies();
		//			//dummies = new Dummies();
		//
		//			dummies = new EventDAO();
		//			this.mEventList=dummies.getAllEvents();


		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.event_list, container,false);
		new DownloadEventsAsync().execute();
		return view;
	}

	private class DownloadEventsAsync extends AsyncTask<Void, Void, List<Event>>{
		ProgressDialog myPd_ring;

		@Override
		protected void onPreExecute() {
			myPd_ring  = new ProgressDialog (getActivity());
			myPd_ring.setMessage("loading events");
			myPd_ring.show();
		}

		@Override
		protected List<Event> doInBackground(Void... arg0) {
			//			 Event[] events = database.getAllEvent();
			//			 for(int i=0; i<events.length; i++){
			//				 mEventList.add(events[i]);
			//				 Log.i("EventListFragment", events[i].toString());
			//			 }
			return StartApplication.getAllEvents();
		}

		@Override
		protected void onPostExecute(List<Event> result) {
			super.onPostExecute(result);

			myPd_ring.dismiss();

			adapter = new EventListAdapter(result, getActivity());

			setListAdapter(adapter);
			
//			 Log.i("onCreateView", "end function");

		}


	}

}
